package com.wzapp.sitposture.usercenter.repository;

import com.wzapp.sitposture.usercenter.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

/**
 * Created by qianglong on 2018/1/16.
 */
@RepositoryRestResource(path = "users", exported = false)
public interface UserRepository extends JpaRepository<User, String>, QueryDslPredicateExecutor {

    /**
     * 功能: 通过手机号查询用户信息<br/>
     *
     * @param mobile
     * @return User
     * @author ql
     * @create 2018-05-08 10:56
     **/
    User findByMobile(String mobile);


    User findOneByAccount(String lowcaseUsername);

    /**
     * 根据邀请码获取用户信息，忽略大小写
     * @param recommendCode
     * @return
     */
    User findTop1ByRecommendCodeIgnoreCase(String recommendCode);

    /**
     * 查询待付费用户
     * @param freeDays
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT a.* FROM user_info a where not EXISTS (SELECT 1 from user_order b where" +
            " a.id = b.user_id) and DATE(a.create_time) <= CURRENT_DATE - ?1")
    List<User> findNewUserByCreateTime(Integer freeDays);

    /**
     * 查询未付费用户
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT a.id FROM user_info a where a.ios_user =0 and EXISTS (SELECT 1 from user_order b where " +
            "  a.id = b.user_id and b.status=0 and DATE(b.create_time) <= CURRENT_DATE - 1) order by a.create_time ")
    List<String> findUnpaidUserByCreateTime();

}
